package com.pt25.study.example.interfaces.facade.test;

import com.datastax.driver.core.*;
import com.pt25.base.dep.cassandra.CassandraService;
import com.pt25.base.model.common.constants.CassandraConstant;
import com.pt25.base.res.ResBody;
import com.pt25.base.util.JsonUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Tag(name = "cassandra test api")
@RefreshScope
@RestController
@RequestMapping("/test-api/example")
public class CassandraTestController {
    private final static String TEST_CASSANDRA = "/test-cassandra";

    @Resource
    private CassandraService cassandraService;

    @Operation(summary = "测试cassandra相关操作")
    @GetMapping(TEST_CASSANDRA)
    public ResBody testES() throws Exception {
        Cluster cassandraCluster = cassandraService.getCassandraCluster(CassandraConstant.CASSANDRA1);
        Session session = cassandraCluster.connect();
        PreparedStatement queryStmt = session.prepare("select user_id, age from config.aim_sf_1_8 limit 5");
        ResultSet resultSet = session.execute(queryStmt.bind());
        List<Row> rowList = resultSet.all();
        log.info(JsonUtil.toJson(rowList));
        session.close();
        return ResBody.builder().build();
    }
}
